﻿using NPOI.HSSF.UserModel;
using OMG.Fill.Mulberry.Model.Ent;
using OMG.Fill.Mulberry.Service;
using ProjectShare;
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Web;
using System.Web.Http;
namespace OMG.Fill.Mulberry.Model.UI.Controllers
{
    public class MulInterCropsController : ApiController
    {
        MulInterCropsServices mulInterCropsServices = new MulInterCropsServices();
        
        //[ApiActionAuthorize("获取表7桑园套种农作物情况调查表T_MulInterCrops分页列表对象操作")]
        //[ApiActionException]
        public Pager<List<MulInterCropsEntity>> PostPageList(MulInterCropsQuery query)
        {
            return mulInterCropsServices.GetPageList(query);
        }
        public ResResult<EditMulInterCropsModel> PostAllList(MulInterCropsQuery query) {
            return mulInterCropsServices.GetAllList(query);
        }
        /// <summary>
        /// 获取地区统计信息
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public ResResult<List<MulInterCropsStatsModel>> PostStatsList(MulNowModel model)
        {
            return mulInterCropsServices.GetStatsList(model);
        }
        public ResResult<List<MulCropsStatistModel>> PostNewStatsList(MulNowModel model)
        {
            return mulInterCropsServices.GetNewStatsList(model);
        }
        public ResResult<List<MulInterCropsStatsModel>> PostStatsTimeList(MulNowModel model)
        {
            return mulInterCropsServices.GetStatsByTimeList(model);
        }
        public ResResult<List<MulInterCropsStatsModel>> PostStatsDetailList(MulNowModel model)
        {
            return mulInterCropsServices.GetDetailStats(model);
        }
        //[ApiActionAuthorize("新增表7桑园套种农作物情况调查表T_MulInterCrops指定对象操作")]
        //[ApiActionException]
        public ResResult<int> Post(EditMulInterCropsModel model) {
            return mulInterCropsServices.AddOrUpdateList(model);
        }
        //[ApiActionAuthorize("修改表7桑园套种农作物情况调查表T_MulInterCrops指定对象操作")]
        //[ApiActionException]
        public ResResult<int> Put(List<MulInterCropsEntity> model)
        {
            foreach (var item in model) {
                if (item.Quantity == null) {
                    item.Quantity = 0;
                }
            }
            //mulInterCropsServices.DeleteData(model[0].ReportId);
            return mulInterCropsServices.UpdateInfo(model);
        }
        //[ApiActionAuthorize("删除表7桑园套种农作物情况调查表T_MulInterCrops指定对象操作")]
        //[ApiActionException]
        public ResResult<int> Delete(string id)
        {
            return mulInterCropsServices.DeleteInfo(id);
        }
        //[ApiActionAuthorize("获取表7桑园套种农作物情况调查表T_MulInterCrops指定对象操作")]
        //[ApiActionException]
        public ResResult<MulInterCropsEntity> Get(string id)
        {
            return mulInterCropsServices.GetModel(id);
        }

        /// <summary>
        /// 导出下载响应
        /// </summary>
        /// <param name="ReportCode"></param>
        public void GetExcelData(string ReportId, string ReportName)
        {

            //打开完成excel文件并下载
            HttpContext curContext = HttpContext.Current;
            // 设置编码和附件格式
            curContext.Response.AddHeader("Access-Control-Allow-Origin", "*");
            curContext.Response.ContentType = "application/vnd.ms-excel";
            curContext.Response.ContentEncoding = Encoding.UTF8;
            curContext.Response.Charset = "";
            string strFileName = "表6桑园套种农作物情况调查表.xls";
            curContext.Response.AppendHeader("Content-Disposition",
                "attachment;filename=" + HttpUtility.UrlEncode(strFileName, Encoding.UTF8));
            curContext.Response.BinaryWrite(mulInterCropsServices.Method(ReportId).GetBuffer());
            curContext.Response.End();
        }

        public void GetStatsExcelData(string FillInYear, string AreaCode, string Status)
        {
            MulNowModel model = new MulNowModel();
            model.FillInYear = FillInYear;
            model.AreaCode = AreaCode;
            model.Status = Status;
            //打开完成excel文件并下载
            HttpContext curContext = HttpContext.Current;
            // 设置编码和附件格式
            curContext.Response.AddHeader("Access-Control-Allow-Origin", "*");
            curContext.Response.ContentType = "application/vnd.ms-excel";
            curContext.Response.ContentEncoding = Encoding.UTF8;
            curContext.Response.Charset = "";
            string strFileName = FillInYear +  "桑园套种农作物情况调查表.xls";
            curContext.Response.AppendHeader("Content-Disposition",
                "attachment;filename=" + HttpUtility.UrlEncode(strFileName, Encoding.UTF8));
            curContext.Response.BinaryWrite(mulInterCropsServices.MethodStats(model).GetBuffer());
            curContext.Response.End();
        }
    }
}